package git.soulbgm.udf;

import org.apache.flink.table.annotation.DataTypeHint;
import org.apache.flink.table.annotation.InputGroup;
import org.apache.flink.table.functions.ScalarFunction;

/**
 * 生成雪花ID的UDF
 *
 * @author SoulBGM
 * @date 2024-09-15
 */
public class SnowflakeUDF extends ScalarFunction {
    private final Sequence sequence;

    public SnowflakeUDF() {
        this.sequence = new Sequence();
    }

    public SnowflakeUDF(long workerId, long datacenterId) {
        this.sequence = new Sequence(workerId, datacenterId);
    }

    public long eval(@DataTypeHint(inputGroup = InputGroup.ANY) Object input) {
        return sequence.nextId();
    }

}
